<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    :destroyOnClose="true"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="分类名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'name',{}]" placeholder="请输入分类名称" :readOnly="disableSubmit"></a-input>
        </a-form-item>
        <a-form-item label="分类编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'code', validatorRules.code]" placeholder="请输入分类编码">
          </a-input>
        </a-form-item>

        <a-form-item label="编码规则" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select
            style="width: 100%"
            placeholder="请选择编码规则"
            showSearch
            optionFilterProp="children"
            :autoClearSearchValue="false"
            allowClear
            v-decorator="['fillRuleId',validatorRules.fillRuleId]"
            :getPopupContainer="(target) => target.parentNode">
            <a-select-option v-for="(item, index) in fillRuleList" :key="index" :value="item.id">
              {{ item.ruleName }}
            </a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

  import pick from 'lodash.pick'
  import { saveOrUpdateCategoryRule } from '../service.js'
  import { listFillRules } from '../../fillRule/service.js'

  export default {
    name: 'SysCategoryRuleModal',
    components: {

    },
    data () {
      return {
        form: this.$form.createForm(this),
        title: '操作',
        width: 800,
        visible: false,
        disableSubmit: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },

        confirmLoading: false,
        validatorRules: {
           code: {
            rules: [{
              required: true, message: '请输入分类编码!'
            }]
          },
          fillRuleId: { rules: [{ required: true, message: '请输入编码规则' }] }
        },
        url: {
        },
        fillRuleList: []
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.categoryRule = record;
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'name', 'code', 'fillRuleId'))
        })
        this.initFillRuleList();
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let formData = Object.assign(this.model, values);
            console.log('表单提交数据', formData)
            let obj = saveOrUpdateCategoryRule(formData)
            obj.then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.submitSuccess(formData)
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
        })
      },
      submitSuccess(formData) {
        this.$emit('ok', formData);
      },
      handleCancel () {
        this.close()
      },

      initFillRuleList() {
        listFillRules().then(res => {
          if (res.success) {
            this.fillRuleList = res.result;
          } else {
            this.fillRuleList = [];
          }
        })
      }
      // handleRuleChange(e) {
      //    this.form.setFieldsValue('fillRuleId', null);
      //   if (e) {
      //     this.codeIsRequired = false;
      //   } else {
      //     this.codeIsRequired = true;
      //   }
      // }
    }
  }
</script>
